/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.base.fragments;

import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.dataoke.shoppingguide.app773026.bean.CategoryBean;

import com.dataoke.shoppingguide.app773026.R;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public abstract class BasePagerFragment extends BaseFragment {
    protected TabLayout mTabLayout;
    protected ViewPager mViewPager;
    protected Adapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_base_view_pager;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        mTabLayout = root.findViewById(R.id.tabLayout);
        mViewPager = root.findViewById(R.id.viewPager);
        mAdapter = new Adapter(getChildFragmentManager());
        mAdapter.resetFragment(getFragments());
        mAdapter.resetBean(getBeans());
        mViewPager.setAdapter(mAdapter);
        if (mTabLayout != null) {
            mTabLayout.setupWithViewPager(mViewPager);
            setupTabView();
        }

    }

    protected void setupTabView() {

    }

    @Override
    protected void initData() {

    }

    public static class Adapter extends FragmentPagerAdapter {
        private List<Fragment> mFragment = new ArrayList<>();
        private Fragment mCurFragment;
        private List<CategoryBean> categoryBeans;

        Adapter(FragmentManager fm) {
            super(fm);
        }

        public void resetFragment(List<Fragment> fragments) {
            mFragment.clear();
            mFragment.addAll(fragments);
        }

        @Override
        public void setPrimaryItem(ViewGroup container, int position, Object object) {
            super.setPrimaryItem(container, position, object);
            if (object instanceof Fragment) {
                mCurFragment = (Fragment) object;
            }
        }

        public Fragment getCurFragment() {
            return mCurFragment;
        }

        public void resetBean(List<CategoryBean> categoryBeans) {
            this.categoryBeans = categoryBeans;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragment.get(position);
        }

        @Override
        public int getCount() {
            return mFragment.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return categoryBeans.get(position).getName();
        }
    }

    protected abstract List<Fragment> getFragments();

    protected abstract List<CategoryBean> getBeans();
}
